package com.librariy.util;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;

public class PermHelper {
    public static final int PERMISSION_REQUEST_CODE_LOCATION = 11;

    public static boolean checkAndRequestPermissions(Fragment mFragment,int requestCode,String[] permissions) {
        Activity mActivity=mFragment.getActivity();
        if(mActivity==null) return false;
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(mActivity, permission) != PackageManager.PERMISSION_GRANTED) {
                mFragment.requestPermissions(permissions, requestCode);
                return false;
            }
        }
        return true;
    }
    public static boolean hasPermission(Context mContext, String... permissions) {
        if(mContext==null) return false;
        if(permissions==null) return true;
        for (String perm: permissions) {
            if(ContextCompat.checkSelfPermission(mContext, perm) != PackageManager.PERMISSION_GRANTED) return false;
        }
        return true;
    }
    public static boolean shouldShowRequestPermissionRationale(Activity mActivity, String permission) {
        if(mActivity==null) return false;
        return (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permission));
    }
     public static boolean checkAndRequestPermissions(Activity mActivity,int requestCode,String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(mActivity, permission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mActivity, permissions, requestCode);
                return false;
            }
        }
        return true;
    }
    public static boolean checkResult(Context mContext,String[] permissions, int[] grantResults) {
         for (int i=0;i<permissions.length&&i<grantResults.length;i++) {
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                if(Manifest.permission.ACCESS_COARSE_LOCATION.equals(permissions[i])
                    || Manifest.permission.ACCESS_FINE_LOCATION.equals(permissions[i])) {
                    UIHelper.showToast(mContext, "请开启定位权限，否则无法使用当前功能！");
                } else if(Manifest.permission.CAMERA.equals(permissions[i])) {
                    UIHelper.showToast(mContext, "请开启相机权限，否则无法拍照！");
                } else if(Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[i])
                    || Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[i])) {
                    UIHelper.showToast(mContext, "请开启文件读写权限，否则无法选取照片！");
                } else {
                    UIHelper.showToast(mContext, "请同意本次申请的全部权限，否则当前功能不可用！");
                }
                return false;
            }
        }
        return true;
    }
}
